from typing import Optional, List
from dataclasses import dataclass

@dataclass(frozen=True)
class TreeNode:
    """不可变二叉树节点"""
    value: int
    left: Optional['TreeNode'] = None
    right: Optional['TreeNode'] = None

    def insert(self, value: int) -> 'TreeNode':
        """插入新值，返回新的树"""
        if value < self.value:
            new_left = self.left.insert(value) if self.left else TreeNode(value)
            return TreeNode(self.value, new_left, self.right)
        else:
            new_right = self.right.insert(value) if self.right else TreeNode(value)
            return TreeNode(self.value, self.left, new_right)

if __name__ == "__main__":
    root = TreeNode(5)
    root = root.insert(3)
    root = root.insert(7)
    root = root.insert(2)
    root = root.insert(4)

    print(root)